package flc.ast.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.MessageAdapter;
import flc.ast.bean.MyDebugBean;
import flc.ast.bean.MyMessageBean;
import flc.ast.databinding.ActivityUdpClientBinding;
import flc.ast.dialog.FormatDialog;
import flc.ast.utils.TCPClient;
import flc.ast.utils.UDPClient;

public class UdpClientActivity extends BaseAc<ActivityUdpClientBinding> {

    public static MyDebugBean debugBean;
    private boolean isConnect;
    private UDPClient udpClient;
    private MessageAdapter messageAdapter;
    private List<MyMessageBean> listMessage = new ArrayList<>();
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss.SSS");

    @Override
    protected int onCreate() {
        return R.layout.activity_udp_client;
    }

    @Override
    protected void initView() {
        mDataBinding.ivUdpClientBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvContentEncode.setOnClickListener(this);
        mDataBinding.tvContentClear.setOnClickListener(this);
        mDataBinding.tvSendEncode.setOnClickListener(this);
        mDataBinding.tvSendClear.setOnClickListener(this);
        mDataBinding.tvSendMessage.setOnClickListener(this);

        udpClient = new UDPClient();

        mDataBinding.rvMessageList.setLayoutManager(new LinearLayoutManager(mContext));
        messageAdapter = new MessageAdapter();
        mDataBinding.rvMessageList.setAdapter(messageAdapter);
        messageAdapter.setList(listMessage);

        udpClient.setClientListener(new UDPClient.ClientListener() {
            @Override
            public void onConnected() {
                udpClient.sendMessage(getString(R.string.conn_text));
            }

            @Override
            public void onConnectionFailed(String error) {
                ToastUtils.showShort(getString(R.string.conn_def) + error);
            }

            @Override
            public void onDisconnected() {

            }

            @Override
            public void onMessageReceived(String message) {
                listMessage.add(new MyMessageBean(TimeUtils.getNowString(simpleDateFormat) + "->", message, 1));
                messageAdapter.setList(listMessage);
                mDataBinding.rvMessageList.smoothScrollToPosition(listMessage.size() - 1);
            }

            @Override
            public void onError(String error) {
                ToastUtils.showShort(error);
            }

            @Override
            public void onStatisticsUpdated(int receivedCount, int sentCount, double receiveSpeed, double sendSpeed, int failedCount) {
                mDataBinding.tvReceive.setText(String.valueOf(receivedCount));
                mDataBinding.tvReceiveSpeed.setText(((int) receiveSpeed) + getString(R.string.byte_speed_unit));
                mDataBinding.tvSend.setText(String.valueOf((sentCount - 1)));
                mDataBinding.tvSendSpeed.setText(((int) sendSpeed) + getString(R.string.byte_speed_unit));
                mDataBinding.tvError.setText(String.valueOf(failedCount));
            }
        });

        udpClient.connect(debugBean.getHost(), Integer.parseInt(debugBean.getPort()));
    }

    @Override
    protected void onClickCallback(View v) {
        int id = v.getId();
        if (id == R.id.tvContentEncode) {//接收消息编码
            showContentFormatDialog();
        } else if (id == R.id.tvContentClear) {//接收消息清空
            listMessage.clear();
            messageAdapter.setList(listMessage);
        } else if (id == R.id.tvSendEncode) {//发送消息编码
            showSendFormatDialog();
        } else if (id == R.id.tvSendClear) {//发送消息清空
            mDataBinding.etMessage.setText("");
        } else if (id == R.id.tvSendMessage) {//发送消息
            String message = mDataBinding.etMessage.getText().toString();
            if (TextUtils.isEmpty(message)) {
                ToastUtils.showShort(getString(R.string.please_input));
                return;
            }
            if (!udpClient.isConnected()) {
                ToastUtils.showShort(getString(R.string.please_connect_server));
                return;
            }
            udpClient.sendMessage(message);
            listMessage.add(new MyMessageBean(TimeUtils.getNowString(simpleDateFormat) + "->", message, 2));
            messageAdapter.setList(listMessage);
            mDataBinding.rvMessageList.smoothScrollToPosition(listMessage.size() - 1);
        }
    }

    @Override
    protected void initData() {
        mDataBinding.tvUdpClientTitle.setText(debugBean.getName() + "\n" + debugBean.getHost() + ":" + debugBean.getPort());
        mDataBinding.tvReceive.setText(String.valueOf(0));
        mDataBinding.tvReceiveSpeed.setText(0 + getString(R.string.byte_speed_unit));
        mDataBinding.tvSend.setText(String.valueOf(0));
        mDataBinding.tvSendSpeed.setText(0 + getString(R.string.byte_speed_unit));
        mDataBinding.tvError.setText(String.valueOf(0));
    }

    private void showContentFormatDialog() {
        FormatDialog dialog = new FormatDialog(mContext);
        dialog.setFormat(mDataBinding.tvContentEncode.getText().toString());
        dialog.setListener(new FormatDialog.Ilistener() {
            @Override
            public void clickConfirm(String format) {
                mDataBinding.tvContentEncode.setText(format);
                udpClient.setReceiveCharsetName(format.equals(getString(R.string.encode_text1)) ? getString(R.string.encode_text2) : format);
            }
        });
        dialog.show();
    }

    private void showSendFormatDialog() {
        FormatDialog dialog = new FormatDialog(mContext);
        dialog.setFormat(mDataBinding.tvSendEncode.getText().toString());
        dialog.setListener(new FormatDialog.Ilistener() {
            @Override
            public void clickConfirm(String format) {
                mDataBinding.tvSendEncode.setText(format);
                udpClient.setSendCharsetName(format.equals(getString(R.string.encode_text1)) ? getString(R.string.encode_text2) : format);
            }
        });
        dialog.show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (udpClient != null) {
            udpClient.disconnect();
        }
    }

}